using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class JournalDAL
    {
        public static int Save(JournalInfo info)
        {
            string sql = string.Format("insert into [journal]([UserName],[IP],[CName],[CreateDate]) values(@UserName,@IP,@CName,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@IP", info.IP),
                new SqlParameter("@CName", info.CName),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(JournalInfo info)
        {
            string sql = string.Format("update [journal] set [UserName]=@UserName,[IP]=@IP,[CName]=@CName where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@IP", info.IP),
                new SqlParameter("@CName", info.CName),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static JournalInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[journal]");
            return Simple_Data(dr);
        }
        public static List<JournalInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[journal]");
            List<JournalInfo> list = new List<JournalInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<JournalInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [journal].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [journal].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[journal]", out count);
            List<JournalInfo> list = new List<JournalInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<JournalInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [journal]"));
            List<JournalInfo> list = new List<JournalInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        private static JournalInfo Simple_Data(DataRow dr)
        {
            JournalInfo info = new JournalInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.IP = DataConvert.ToString(dr["IP"]);
                info.CName = DataConvert.ToString(dr["CName"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
    }
}

